package org.dromara.system.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.system.domain.vo.WbCompanyBlacklistVo;
import org.dromara.system.listener.WbCompanyBlacklistImportListener;
import org.dromara.system.listener.WbMinuteListListener;
import org.dromara.system.service.IWbCompanyInfoService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.WbMinuteListVo;
import org.dromara.system.domain.bo.WbMinuteListBo;
import org.dromara.system.service.IWbMinuteListService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 中标分包详情
 *
 * @author Lion Li
 * @date 2024-05-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/minuteList")
public class WbMinuteListController extends BaseController {

    private final IWbMinuteListService wbMinuteListService;
    private final IWbCompanyInfoService wbCompanyInfoService;

    /**
     * 查询中标分包详情列表
     */
    @SaCheckPermission("system:minuteList:list")
    @GetMapping("/list")
    public TableDataInfo<WbMinuteListVo> list(WbMinuteListBo bo, PageQuery pageQuery) {
        return wbMinuteListService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出中标分包列表模板
     */
    @SaCheckPermission("system:minuteList:export")
    @Log(title = "中标分包内容", businessType = BusinessType.EXPORT)
    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<WbMinuteListVo> list = new ArrayList<>();
        WbMinuteListVo wbMinuteListVo = new WbMinuteListVo();
        wbMinuteListVo.setSubStandardId("0424C1-9200000-XXXX");
        wbMinuteListVo.setSubStandardName("分标名");
        wbMinuteListVo.setBagNo("包号");
        wbMinuteListVo.setBagName("保命");
        wbMinuteListVo.setCompanyName("中标公司名称");
        wbMinuteListVo.setPrice("30.0f");
        list.add(wbMinuteListVo);
        ExcelUtil.exportExcel(list, "中标分包内容", WbMinuteListVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     */
    @Log(title = "中标分包内容", businessType = BusinessType.IMPORT)
    @SaCheckPermission("system:minuteList:add")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file ,Long wbMinuteBidBagId) throws Exception {
        ExcelResult<WbMinuteListVo> result = ExcelUtil.importExcel(file.getInputStream(), WbMinuteListVo.class,new WbMinuteListListener(wbMinuteBidBagId));
        return R.ok(result.getAnalysis());
    }



    /**
     * 导出中标分包详情列表
     */
    @SaCheckPermission("system:minuteList:export")
    @Log(title = "中标分包详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WbMinuteListBo bo, HttpServletResponse response) {
        List<WbMinuteListVo> list = wbMinuteListService.queryList(bo);
        ExcelUtil.exportExcel(list, "中标分包详情", WbMinuteListVo.class, response);
    }

    /**
     * 获取中标分包详情详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:minuteList:query")
    @GetMapping("/{id}")
    public R<WbMinuteListVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(wbMinuteListService.queryById(id));
    }

    /**
     * 新增中标分包详情
     */
    @SaCheckPermission("system:minuteList:add")
    @Log(title = "中标分包详情", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WbMinuteListBo bo) {
        wbCompanyInfoService.handleName(bo.getCompanyName());
        return toAjax(wbMinuteListService.insertByBo(bo));
    }

    /**
     * 修改中标分包详情
     */
    @SaCheckPermission("system:minuteList:edit")
    @Log(title = "中标分包详情", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WbMinuteListBo bo) {
        wbCompanyInfoService.handleName(bo.getCompanyName());
        return toAjax(wbMinuteListService.updateByBo(bo));
    }

    /**
     * 删除中标分包详情
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:minuteList:remove")
    @Log(title = "中标分包详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wbMinuteListService.deleteWithValidByIds(List.of(ids), true));
    }
}
